<?php

use Nette\Security as NS, Models as MO;


/**
 * Users authenticator.
 *
 * @author     John Doe
 * @package    MyApplication
 */
class Authenticator extends MO\Base implements NS\IAuthenticator
{

        public function authenticate(array $credentials)
        {
            list($username, $password) = $credentials;
            $row = $this->db->query("
                SELECT acc.heslo as heslo,
                       acc.login as login,
                       cle.nick as nick,
                       cle.id as id
                       
                FROM pov_clenove_acc acc
                LEFT JOIN pov_clenove cle ON(cle.id=acc.clen)
                WHERE login = '".$username."'
                                    ");
            $pass;
            foreach ($row as $key) {
                $pass=$key;
            }   
            if (!$pass) {
                throw new NS\AuthenticationException("Uživatel '$username' nenalezen.", self::IDENTITY_NOT_FOUND);
            }
            if ($pass->heslo !== $password) { //TODO: $this->calculateHash($password)) {
                throw new NS\AuthenticationException("Špatné heslo!", self::INVALID_CREDENTIAL);
            }
            unset($pass->heslo);
            return new NS\Identity($pass->id, NULL, $pass->toArray());
         
        }
        
	public function calculateHash($password)
	{
		return md5($password . str_repeat('*enter any random salt here*', 10));
	}

        

}
